package cn.congzhou.crm.web.controller;

import cn.congzhou.crm.domain.Department;
import cn.congzhou.crm.domain.School;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.service.IDepartmentService;
import cn.congzhou.crm.service.ISchoolService;
import cn.congzhou.crm.utils.ResultJSON;
import cn.congzhou.crm.utils.UserContext;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/school")
public class SchoolController {
    private Logger logger = LoggerFactory.getLogger(SchoolController.class);

    @Autowired
    private ISchoolService schoolService;

    @Autowired
    private IDepartmentService departmentService;


    @RequestMapping(value = "/toSchoolGrid",method = RequestMethod.GET)
    @RequiresPermissions(value = {"查询校区信息","school:query"},logical = Logical.OR)
    public String toSchoolGrid(){
        return "/school/schoolGrid";
    }


    @RequestMapping(value = "/getSchoolData",method = RequestMethod.POST)
    @RequiresPermissions(value = {"查询校区信息","school:query"},logical = Logical.OR)
    public void getSchoolData(HttpServletResponse response){
        List<School> schools = schoolService.selectAll();
        Map<String, Object> map = new HashMap<>();
        map.put("list",schools);
        String json = JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.println(json);
            writer.flush();
        } catch (Exception e) {
            logger.error("查询校区信息出错",e);
        }finally {
            if(writer != null){
                try{
                    writer.close();
                }catch (Exception e){}
            }
        }
    }
    @ResponseBody
    @RequestMapping(value = "/getDeptData",method = RequestMethod.POST)
    //在新增修改校区信息的时候后需要加载部门信息
    @RequiresPermissions(value = {"新增/修改校区信息","school:saveOrUpdate"},logical = Logical.OR)
    public Object getDepartmentData(){
        List<Department> departments = departmentService.selectAll();
        return departments;
    }

    @ResponseBody
    @RequestMapping(value = "/saveSchool",method = RequestMethod.POST)
    @RequiresPermissions(value = {"新增/修改校区信息","school:saveOrUpdate"},logical = Logical.OR)
    public Object saveSchool(School school,Long[] deptIds){
        ResultJSON result = new ResultJSON();
        if(school == null){
            result.mark("提交的校区信息为空");
            return result;
        }
        try {
            schoolService.saveOrUpdate(school,deptIds);

            logger.info(UserContext.getCurrentLoginUserAndIp() + "修改了校区信息：" + school.getName());
        }catch (Exception e){
            result.mark("保存校区信息出错");
            logger.error("保存校区信息出错" + e.getMessage(),e);
        }

        return result;
    }

}
